<template>
  <div class="user">
    <div class="title">
      <span class="word">管理员信息</span>
    </div>
    <div class="line"></div>
    <div class="middle">
      <div>
        <el-input
          v-model="state.input3"
          class="w-50 m-2"
          size="small"
          placeholder="请输入姓名、手机号、邮箱搜索"
        />
      </div>
      <div class="topBtn">
        <el-button @click="adduser = true">新建账户</el-button>
        <el-button @click="handlePi">批量删除</el-button>
      </div>
    </div>
    <div class="table">
      <el-table
        :data="state.tableData"
        style="width: 100%"
        border="true"
        :header-cell-style="{
          background: '#b4c7e7',
          color: 'black',
          fontSize: '12px',
          fontWeight: '500',
          textAlign: 'center',
        }"
        @selection-change="handleSelectionChanges"
      >
        <el-table-column type="selection" align="center" />
        <el-table-column prop="adminNumber" label="员工工号" align="center" />
        <el-table-column prop="adminName" label="姓名" align="center" />
        <el-table-column prop="adminEmail" label="邮箱" align="center" />
        <el-table-column prop="adminTel" label="手机号/用户名" align="center" />
        <el-table-column prop="roleName" label="角色" align="center" />
        <el-table-column label="项目绑定" align="center">
          <template v-slot="scope">
            <el-button
              type="text"
              @click="handleBinds(scope.row.adminId)"
              align="center"
              >绑定</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template v-slot="scope">
            <el-switch
              v-model="scope.row.status"
              @click="handleSwitch(scope.row.status, scope.row.adminId)"
              :active-value="1"
              :inactive-value="0"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间 "
          header-align="center"
        />
        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <el-button
              type="text"
              class="btn"
              @click="handleCheck(scope.row.adminId), (checkUser = true)"
              >查看</el-button
            >
            <el-button
              type="text"
              class="btn"
              @click="handleEdit(scope.row), (edit = true)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增账户 -->
    <div class="addUserDialog addDialog">
      <el-dialog v-model="adduser" title="新建账户">
        <div class="line3"></div>

        <el-form :model="state.form">
          <el-form-item label="员工号" :label-width="formLabelWidth">
            <el-input
              v-model="state.form.adminNumber"
              autocomplete="off"
              placeholder="请输入工号"
            />
          </el-form-item>
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input
              v-model="state.form.adminName"
              autocomplete="off"
              placeholder="请输入姓名"
            />
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input
              v-model="state.form.adminPwd"
              autocomplete="off"
              placeholder="请输入密码"
            />
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth">
            <el-input
              v-model="state.form.adminEmail"
              autocomplete="off"
              placeholder="请输入邮箱"
            />
          </el-form-item>
          <el-form-item label="手机号" :label-width="formLabelWidth">
            <el-input
              v-model="state.form.adminTel"
              autocomplete="off"
              placeholder="请输入手机号"
            />
          </el-form-item>
          <el-form-item label="角色" :label-width="formLabelWidth">
            <el-select
              v-model="state.form.roleId"
              class="m-2"
              placeholder="请输入角色"
              size="small"
            >
              <el-option
                v-for="item in state.options"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" :label-width="formLabelWidth">
            <el-switch v-model="state.form.status" />
          </el-form-item>
        </el-form>
        <div class="line"></div>

        <template #footer>
          <span class="dialog-footer">
            <el-button @click="(adduser = false), (cpwd = false)" class="cancel"
              >取消</el-button
            >
            <el-button type="primary" @click="postUserAdd" class="save"
              >保存</el-button
            >
          </span>
        </template>
      </el-dialog>
    </div>
    <!-- 绑定项目 -->
    <div class="binduserDialog bindDialog">
      <el-dialog v-model="binds" title="绑定项目">
        <div class="line3"></div>
        <div class="table2">
          <el-table
            :data="
              state.tableDatas.slice(
                (currentPage - 1) * pagesize,
                currentPage * pagesize
              )
            "
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" />
            <el-table-column
              prop="projectName"
              label="项目名称"
              align="center"
            />
            <el-table-column prop="city" label="城市/省份" align="center" />
            <el-table-column prop="functions" label="功能" align="center" />
            <el-table-column prop="owner" label="业主名称" align="center" />
            <el-table-column prop="point" label="总点数" align="center" />
            <el-table-column
              prop="onlineTime"
              label="上线时间"
              align="center"
            />
          </el-table>
        </div>
        <div class="page">
          <div class="example-pagination-block">
            <el-pagination
              layout="prev,pager,next"
              :total="state.tableData.length"
              @size-change="handleSizeChange"
              :current-page="currentPage"
            />
          </div>
        </div>

        <div class="line"></div>

        <template #footer>
          <span class="dialog-footer">
            <el-button @click="(binds = false), (cpwd = false)" class="cancel"
              >取消</el-button
            >
            <el-button
              type="primary"
              @click="(binds = false), (cpwd = false), handleBindes()"
              class="save"
              >保存</el-button
            >
          </span>
        </template>
      </el-dialog>
    </div>
    <!-- 查看 -->
    <div class="checkuserDialog checkDialog">
      <el-dialog v-model="checkUser" title="查看账户">
        <div class="line3"></div>
        <div class="contant">
          <el-row :gutter="16">
            <el-col :span="6">
              <span>员工工号：{{ state.details.adminNumber }}</span>
            </el-col>
            <el-col :span="6">
              <span>姓名:{{ state.details.adminName }}</span>
            </el-col>
            <el-col :span="6">
              <span>邮箱:{{ state.details.adminEmail }}</span>
            </el-col>
          </el-row>
          <el-row :gutter="16">
            <el-col :span="6">
              <span>手机号：{{ state.details.adminTel }}</span>
            </el-col>
            <el-col :span="6">
              <span>角色：{{ state.details.roleName }}</span>
            </el-col>
            <el-col :span="6">
              <span
                >状态：{{ state.details.status == 1 ? "启用" : "禁用" }}</span
              >
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="21">
              <span>已绑定项目</span>
            </el-col>
          </el-row>
        </div>
        <div class="table2">
          <el-table :data="state.projects" style="width: 100%">
            <el-table-column
              prop="projectName"
              label="项目名称"
              align="center"
            />
            <el-table-column prop="city" label="城市/省份" align="center" />
            <el-table-column prop="functions" label="功能" align="center" />
            <el-table-column prop="owner" label="业主名称" align="center" />
            <el-table-column prop="point" label="总点数" align="center" />
            <el-table-column
              prop="onlineTime"
              label="上线时间"
              align="center"
            />
          </el-table>
        </div>
      </el-dialog>
    </div>
    <!-- 编辑 -->
    <div class="checkuserDialog checkDialog">
      <el-dialog v-model="edit" title="编辑账户">
        <div class="line3"></div>
        <div class="contant">
          <el-row :gutter="24">
            <el-col :span="6">
              <div class="box">
                <div class="word">工号:</div>
                <el-input v-model="state.editDatas.adminNumber"></el-input>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="box">
                <div class="word">姓名:</div>
                <el-input v-model="state.editDatas.adminName"></el-input>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="box">
                <div class="word">邮箱:</div>
                <el-input v-model="state.editDatas.adminEmail"></el-input>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="16">
            <el-col :span="6">
              <div class="box">
                <div class="word">手机号：</div>
                <el-input v-model="state.editDatas.adminTel"></el-input>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="box">
                <div class="word">角色：</div>
                <el-select
                  v-model="state.editDatas.roleId"
                  class="m-2"
                  placeholder="Select"
                  size="small"
                >
                  <el-option
                    v-for="item in state.options"
                    :key="item.roleId"
                    :label="item.roleName"
                    :value="item.roleId"
                  />
                </el-select>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="box">
                <div class="word">状态：</div>
                <el-switch
                  v-model="state.editDatas.status"
                  :active-value="1"
                  :inactive-value="2"
                />
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="21">
              <span>已绑定项目</span>
              <span @click="bindlog(state.editDatas)"
                ><el-button class="bd">绑定</el-button></span
              >
            </el-col>
          </el-row>
        </div>
        <div class="table2">
          <el-table :data="state.edit_pro" style="width: 100%">
            <el-table-column
              prop="projectName"
              label="项目名称"
              align="center"
            />
            <el-table-column prop="city" label="城市/省份" align="center" />
            <el-table-column prop="functions" label="功能" align="center" />
            <el-table-column prop="owner" label="业主名称" align="center" />
            <el-table-column prop="point" label="总点数" align="center" />
            <el-table-column
              prop="onlineTime"
              label="上线时间"
              align="center"
            />
            <el-table-column label="操作">
              <template #default="scope">
                <el-button
                  link
                  type="text"
                  size="small"
                  @click="rm(scope.row.projectId)"
                  class="btn"
                  >移除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="(edit = false), (cpwd = false)" class="cancel"
              >取消</el-button
            >
            <el-button type="primary" @click="editUser" class="save"
              >保存</el-button
            >
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import {
  UserList,
  UserAdd,
  RoleList,
  ProjectList,
  Details, // 查询数据不准确
  BindProject,
  Deletes,
  Status,
  EditUser,
} from "@/api/model/user";
import { ElMessage } from "element-plus";
var currentPage = ref(1);
var pagesize = ref(10);

const adduser = ref(false);
const checkUser = ref(false);
const edit = ref(false);
const binds = ref(false);
const formLabelWidth = ref(70);

const state = reactive({
  tableDatas: [],

  tableData: [],

  input3: "",

  form: {
    status: "2",
  },

  editDatas: {},

  options: [],
  // 项目id
  // pro_id: []

  // 查看详情
  details: {},
  projects: [],

  // 用户id
  admin_d: 0,

  // 用户状态
  statue: false,

  // 修改对话框的项目列表
  edit_pro: [],
});

const pro_id = ref("");

const user_id = ref("");

function handleEdit(b) {
  console.log(b, "Aaaaaaaaaaaaaad");
  state.editDatas = b;
  state.edit_pro = b.project;
  state.editDatas.projectId = b.projectId;
}
const rm = (id) => {
  state.edit_pro = state.edit_pro.filter((i) => i.projectId != id);
  state.editDatas.projectId = state.editDatas.projectId
    .split(",")
    .filter((i) => i != id)
    .join(",");
  console.log(state.editDatas, "xx");
};
// 查看详情
const handleCheck = async (a) => {
  let { data: res } = await Details({ adminId: a });
  console.log("查看详情", res);
  if (res.code == 200) {
    state.details = res.data.list[0];
    state.projects = res.data.list[0].project;
  }
};

// 获取项目列表
async function bindlog(val) {
  state.admin_d = val.adminId;
  let { data: res } = await ProjectList();
  console.log("获取项目列表", res);
  if (res.code == 200) {
    state.tableDatas = res.data.list;
  }

  edit.value = false;
  binds.value = true;
}
onMounted(() => {
  getUserList();
  getRoleList();
});

// 获取用户列表
const getUserList = async () => {
  let { data: res } = await UserList({ keyword: state.input3 });
  console.log(res, "获取用户列表");
  if (res.code == 200) {
    state.tableData = res.data.list;
  }
};

// 获取所有角色
const getRoleList = async () => {
  let { data: res } = await RoleList();

  if (res.code == 200) {
    state.options = res.data.list;
    console.log(state.options, "获取所有角色");
  }
};

// 添加用户
const postUserAdd = async () => {
  if (state.form.status == false) {
    state.form.status = "2";
  } else if (state.form.status == true) {
    state.form.status = "1";
  }
  let res = await UserAdd(state.form);
  console.log("添加用户", res);
  if (res.status == 200) {
    adduser.value = false;
    state.form = {};
    ElMessage({
      message: "添加成功",
      type: "success",
    });
    getUserList();
  }
};

// 编辑用户
const editUser = async () => {
  // state.editDatas.headportrait = "1";

  delete state.editDatas.adminPwd;
  // state.editDatas.admin_pwd = "123456";
  console.log(state.editDatas, 6666666);
  let { data: res } = await EditUser(state.editDatas);

  if (res.code == 200) {
    ElMessage({
      message: "修改成功",
      type: "success",
    });
    getUserList();
    edit.value = false;
  }
};
// 修改状态
const handleSwitch = async (s, id) => {
  console.log("修改状态", s, id);
  let { data: res } = await Status({ adminId: id, status: s });
  console.log(res, "修改状态");
  if (res.code == 200) {
    ElMessage({
      message: "修改状态成功",
      type: "success",
    });
  }
};

// 绑定事件
const handleBinds = async (d) => {
  state.admin_d = d;
  binds.value = true;
  let { data: res } = await ProjectList();
  console.log("绑定事件", res, d, "d");
  if (res.code == 200) {
    state.tableDatas = res.data.list;
  }
};

// 监听绑定项目的多选框
const handleSelectionChange = (val) => {
  let aa = "";
  for (var i = 0; i < val.length; i++) {
    aa += val[i].projectId + ",";
  }
  pro_id.value = aa;
  console.log(aa, " 监听绑定项目的多选框");
  // console.log(pro_id.value)
};

// 监听用户多选框
const handleSelectionChanges = (val) => {
  let aa = "";
  for (var i = 0; i < val.length; i++) {
    aa += val[i].adminId + ",";
  }
  user_id.value = aa;
};

// 批量删除用户
const handlePi = async () => {
  console.log("user_id.value ", user_id.value);
  let { data: res } = await Deletes({ ids: user_id.value });
  console.log(res, "批量删除用户");
  if (res.code == 200) {
    ElMessage({
      message: "批量删除成功",
      type: "success",
    });
    getUserList();
  }
};

// 绑定
const handleBindes = async () => {
  let { data: res } = await BindProject({
    adminId: state.admin_d,
    projectId: pro_id.value,
  });
  if (res.code == 200) {
    ElMessage({
      message: "绑定成功",
      type: "success",
    });
    getUserList();
  }
};
</script>

<style scoped lang="less">
.topBtn {
  .el-button {
    left: 1592px;
    top: 201px;
    width: 104px;
    height: 30px;
    line-height: 20px;
    border-radius: 4px;
    background-color: rgba(143, 170, 220, 100);
    text-align: center;
    color: white;
    border: none;
  }
}
.bd {
  left: 766px;
  top: 469px;
  width: 80px;
  height: 24px;
  line-height: 20px;
  border-radius: 4px;
  background-color: rgba(143, 170, 220, 100);
  text-align: center;
  margin-left: 20px;
  color: white;
  border: none;
}
.word {
  width: 90px;
}
.box {
  display: flex;
  /deep/ .el-input--small .el-input__inner {
    width: 120px;
    height: 24px;
    line-height: 24px;
    padding: 0 7px;
  }
}
.user {
  width: 100%;
  height: 100%;
  border-style: none;
  border-color: unset;
  box-shadow: rgb(77 98 165 / 10%) 0px 5px 20px 0px;
  border-radius: 8px;
  font-size: 14px;
  padding: 0px;
  line-height: 20px;
  font-weight: normal;
  font-style: normal;
  background-color: white;
}
.title {
  padding: 20px;
  .word {
    font-family: SourceHanSansSC;
    font-weight: 550;
    font-size: 20px;
    color: rgb(77, 98, 165);
    font-style: normal;
    letter-spacing: 2px;
    line-height: 28px;
    text-decoration: none;
  }
}
.line {
  border-bottom: 2px rgba(77, 98, 165, 0.2) solid;
}
.middle {
  display: flex;
  justify-content: flex-end;
  align-content: space-between;
  flex-direction: row;
  padding: 20px;
  .el-input {
    margin-right: 20px;
    margin-top: 5px;
  }
}
.btn {
  color: rgb(81, 99, 173);
}
.table {
  padding: 20px;
  // text-align: center;
  .btn {
    color: rgb(81, 99, 173);
  }
}
.addDialog {
  :deep(.el-dialog__title) {
    color: rgb(81, 99, 167);
    font-size: 19px;
  }
  :deep(.el-dialog__close) {
    font-size: 27px !important;
    color: rgb(81, 99, 167);
  }
  :deep(.el-dialog__body) {
    padding: 20px 0 20px;
  }
}
.addUserDialog {
  /deep/.el-dialog {
    border-radius: 8px;
    --el-dialog-width: 410px;
    width: var(--el-dialog-width, 410px);
  }
  /deep/.el-input {
    width: 250px;
    margin-left: 30px;
  }
  .line3 {
    border-bottom: 2px rgba(77, 98, 165, 0.2) solid;
    margin-bottom: 30px;
  }
  .num {
    margin-left: 30px;
  }

  .el-switch {
    margin-left: 30px;
  }
  .cancel {
    left: 917px;
    top: 696px;
    width: 80px;
    height: 30px;
    line-height: 20px;
    border-radius: 4px;
    color: rgba(255, 255, 255, 100);
    background-color: rgba(204, 204, 204, 100);
    text-align: center;
    border: none;
  }
  .save {
    left: 1013px;
    top: 696px;
    width: 80px;
    height: 30px;
    line-height: 20px;
    border-radius: 4px;
    background-color: rgba(143, 170, 220, 100);
    text-align: center;
    border: none;
  }
}
.bindDialog {
  :deep(.el-dialog__title) {
    color: rgb(81, 99, 167);
    font-size: 19px;
  }
  :deep(.el-dialog__close) {
    font-size: 27px !important;
    color: rgb(81, 99, 167);
  }
  :deep(.el-dialog__body) {
    padding: 20px 0 20px;
  }
}
.binduserDialog {
  /deep/.el-dialog {
    border-radius: 8px;
    --el-dialog-width: 729px;
    width: var(--el-dialog-width, 729px);
  }
  .page {
    display: flex;
    justify-content: flex-end;
  }
  .line3 {
    border-bottom: 2px rgba(77, 98, 165, 0.2) solid;
    margin-bottom: 10px;
  }
  .num {
    margin-left: 30px;
  }
  .table2 {
    padding: 10px 20px 10px 20px;
  }

  .cancel {
    left: 917px;
    top: 696px;
    width: 80px;
    height: 30px;
    line-height: 20px;
    border-radius: 4px;
    color: rgba(255, 255, 255, 100);
    background-color: rgba(204, 204, 204, 100);
    text-align: center;
    border: none;
  }
  .save {
    left: 1013px;
    top: 696px;
    width: 80px;
    height: 30px;
    line-height: 20px;
    border-radius: 4px;
    background-color: rgba(143, 170, 220, 100);
    text-align: center;
    border: none;
  }
}
.checkDialog {
  :deep(.el-dialog__title) {
    color: rgb(81, 99, 167);
    font-size: 19px;
  }
  :deep(.el-dialog__close) {
    font-size: 27px !important;
    color: rgb(81, 99, 167);
  }
  :deep(.el-dialog__body) {
    padding: 20px 0 20px;
  }
}
.checkuserDialog {
  background-color: crimson;
  /deep/.el-dialog {
    border-radius: 8px;
    --el-dialog-width: 729px;
    width: var(--el-dialog-width, 729px);
  }

  .line3 {
    border-bottom: 2px rgba(77, 98, 165, 0.2) solid;
    margin-bottom: 30px;
  }
  .num {
    margin-left: 30px;
  }
  .table2 {
    padding: 10px 20px 10px 20px;
  }
}
.contant .el-row {
  display: flex;
  flex-wrap: wrap;
  position: relative;
  box-sizing: border-box;
  justify-content: space-evenly;
  margin-bottom: 10px;
  span {
    color: rgba(127, 127, 127, 100);
    font-size: 14px;
    text-align: left;
    font-family: SourceHanSansSC-regular;
  }
}
</style>
<style scoped lang="less">
/deep/.el-input--small .el-input__suffix {
  left: 270px;
}
/deep/.el-input--small .el-input__inner {
  width: 290px;
  height: 24px;
  line-height: 24px;
  padding: 0 7px;
}
/deep/.el-table .el-table__cell {
  background-color: rgb(245, 248, 251);
  padding: 8px 0;
  min-width: 0;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  z-index: 1;
}
</style>
